<?php
// +----------------------------------------------------------------------
// | OpenCMF [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.opencmf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 廖权名  1528490339@qq.com
// +----------------------------------------------------------------------
namespace Yiban\Admin;

use Admin\Controller\AdminController;
use Common\Util\Think\Page;
use Common\PHPExcel;

/**
 * 新闻控制器
 * @author 廖权名  1528490339@qq.com
 */
class SignAdmin extends AdminController
{
    /**
     * 默认方法
     * @author 廖权名  1528490339@qq.com
     */
    public function index()
    {
        $attr_import['name'] = 'importData';
        $attr_import['title'] = '表格导入';
        $attr_import['class'] = 'label label-success';
        $attr_import['href'] = U('importData',array('id' => '__data_id__'));

        $keyword = I('keyword', '', 'string');
        $condition = array('like', '%' . $keyword . '%');
        $map['id|name'] = array(
            $condition,
            $condition,
            $condition,
            $condition,
            $condition,
            '_multi' => true
        );

        // 获取所有项目
        $map['status'] = array('egt', '0'); // 禁用和正常状态
        $p = !empty($_GET["p"]) ? $_GET['p'] : 1;
        $user_object = M('Yiban_signinitem');
        $data_list = $user_object
            ->page($p, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->order('id desc')
            ->select();
        $page = new Page(
            $user_object->where($map)->count(),
            C('ADMIN_PAGE_ROWS')
        );

        $attr['name'] = 'look';
        $attr['title'] = '查看';
        $attr['class'] = 'label label-success';
        $attr['href'] = U('look', array('id' => '__data_id__'));

        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('学院管理')// 设置页面标题
        ->addTopButton('addnew')// 添加新增按钮
        ->addTopButton('resume', array('model' => 'sport_news'))// 添加启用按钮
        ->addTopButton('forbid', array('model' => 'sport_news'))// 添加禁用按钮
        ->addTopButton('delete', array('model' => 'sport_news'))// 添加删除按钮
        ->setSearch('请输入ID/新闻标题', U('index'))
            ->addTableColumn('id', '编号')
            ->addTableColumn('name', '签到标题')
            ->addTableColumn('status', '状态', 'status')
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list)// 数据列表
            ->setTableDataPage($page->show())// 数据列表分页
            ->addRightButton('self', $attr_import)// 添加编辑按钮
            ->addRightButton('edit')// 添加编辑按钮
            ->addRightButton('self', $attr)
            ->addRightButton('recycle')// 添加删除按钮
            ->display();
    }

    /**
     * 新增新闻
     * @author 廖权名  1528490339@qq.com
     */
    public function add()
    {
        if (IS_POST) {
            $user_object = D('Yiban_signinitem');
            $data = $user_object->create();
            if ($data) {
                $id = $user_object->add();
                if ($id) {
                    $this->success('新增成功', U('index'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($user_object->getError());
            }
        } else {
            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增')//设置页面标题
            ->setPostUrl(U('add'))//设置表单提交地址
            ->addFormItem('name', 'text', '标题', '标题')
                ->display();
        }
    }

    public function look($id)
    {


        print_r($id);
        $keyword = I('keyword', '', 'string');
        $condition = array('like', '%' . $keyword . '%');
        $map['oc_yiban_signindetail.uid|oc_yiban_signindetail.uname'] = array(
            $condition,
            $condition,
            $condition,
            $condition,
            $condition,
            '_multi' => true
        );



        // 获取所有项目
        $map['oc_yiban_signindetail.status'] = array('egt', '0'); // 禁用和正常状态
        $map['oc_yiban_signindetail.uid'] = $id;
        $p = !empty($_GET["p"]) ? $_GET['p'] : 1;
        $user_object = M('Yiban_signindetail');
        $data_list = $user_object
            ->field("oc_yiban_signindetail.*,oc_yiban_signindetail.status as tt,oc_yiban_signinitem.name")
            ->join("oc_yiban_signinitem on oc_yiban_signindetail.uid=oc_yiban_signinitem.id")
            ->page($p, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->select();
        $page = new Page(
            $user_object
                ->join("oc_yiban_signinitem on oc_yiban_signindetail.uid=oc_yiban_signinitem.id")
                ->where($map)
                ->count(),
            C('ADMIN_PAGE_ROWS')
        );


        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('学院管理')// 设置页面标题

            ->addTopButton('addnew')// 添加新增按钮

            ->addTopButton('resume', array('model' => 'sport_news'))// 添加启用按钮
            ->addTopButton('forbid', array('model' => 'sport_news'))// 添加禁用按钮
            ->addTopButton('delete', array('model' => 'sport_news'))// 添加删除按钮
            ->setSearch('请输入ID/新闻标题', U('index'))
            ->addTableColumn('id', '编号')
            ->addTableColumn('name', '签到标题')
            ->addTableColumn('uname', '签到标题')
            ->addTableColumn('status', '状态', 'status')
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list)// 数据列表
            ->setTableDataPage($page->show())// 数据列表分页
            ->addRightButton('edit')// 添加编辑按钮
            ->addRightButton('forbid')// 添加禁用/启用按钮
            ->addRightButton('recycle')// 添加删除按钮
            ->display();

    }

    public function importData($id)
    {

        if (IS_POST) {


            // 提交数据
            $user_object = D('Yiban/Sign');
            $data = $user_object->create();
            $d=M("yiban_signindetail");

            if ($data) {


                import("Org.Util.PHPExcel");
                import("Org.Util.PHPExcel.IOFactory", '', '.php');
                $inputFileName = substr(get_upload_info($data['excelid'])['path'],1);
                date_default_timezone_set('PRC');
// 读取excel文件
                try {
                    $inputFileType = \PHPExcel_IOFactory::identify($inputFileName);
                    $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
                    $objPHPExcel = $objReader->load($inputFileName);
                } catch (Exception $e) {
                    die('加载文件发生错误："' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
                }

// 确定要读取的sheet，什么是sheet，看excel的右下角，真的不懂去百度吧

                $sheet = $objPHPExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow();
                $highestColumn = $sheet->getHighestColumn();

// 获取一行的数据
                for ($row = 1; $row <= $highestRow; $row++) {
// Read a row of data into an array
                    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
//这里得到的rowData都是一行的数据，得到数据后自行处理，我们这里只打出来看看效果


                    //var_dump($rowData[0][0]);
                    if ($rowData[0][0]==null){}
                    else{
                        $da["uname"]=$rowData[0][0];
                        $da["uid"]=$id;
                        $d->add($da);
                    }

                    //echo "<br>";
                }






                $result = $user_object
                    ->field('id,name,excelid')
                    ->save($data);
                if ($result) {

                    //implode(" ",$data)
                    $this->success("导入成功",U('index'));
                } else {
                    $this->error('导入失败', $user_object->getError());
                }
            } else {
                $this->error($user_object->getError());
            }
        } else {
            // 获取账号信息
            $info = D('Yiban/Sign')->find($id);
            //print_r(substr(get_upload_info(9)['path'],1));




            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑用户')// 设置页面标题
            ->setPostUrl(U('importData'))// 设置表单提交地址
            ->addFormItem('id', 'hidden', 'ID', 'ID')
                ->addFormItem('name', 'hidden', '昵称', '昵称')
                ->addFormItem('excelid', 'file', '文件', '文件')
                ->setFormData($info)
                ->display();
        }
    }


    /**
     * 编辑新闻
     * @author 廖权名  1528490339@qq.com
     */
    public function edit($id)
    {
        if (IS_POST) {
            // 提交数据
            $user_object = D('Yiban_signinitem');
            $data = $user_object->create();
            if ($data) {
                $result = $user_object
                    ->field('id,name')
                    ->save($data);
                if ($result) {
                    $this->success('更新成功', U('index'));
                } else {
                    $this->error('更新失败', $user_object->getError());
                }
            } else {
                $this->error($user_object->getError());
            }
        } else {
            // 获取账号信息
            $info = D('Yiban_signinitem')->find($id);

            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑新闻')// 设置页面标题
            ->setPostUrl(U('edit'))// 设置表单提交地址
            ->addFormItem('id', 'hidden', 'ID', 'ID')
                ->addFormItem('name', 'text', '标题', '标题')
                ->setFormData($info)
                ->display();
        }
    }


}
